﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;

namespace PluginZingMe
{
    public partial class frmBlog : Form
    {
        string pos = "tqninh";
        string uid = "";
        string hash = "";
        string bid = "";
        string ownerName = "";
        int index = 0;
        int load = 0;
        ArrayList arrAcc = null;
        ArrayList arrShare = null;
        string[] acc;

        public frmBlog()
        {
            InitializeComponent();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            wbrBlog.Visible = !wbrBlog.Visible;
        }

        private void frmBlog_Load(object sender, EventArgs e)
        {
            txtAccount.Text = "0";
            txtIndex.Text = "0";
            txtText.Text = "0";
            cbRate1Auto.CheckedChanged += cbRate1Auto_CheckedChanged;
            cbRate2Auto.CheckedChanged += cbRate2Auto_CheckedChanged;
            cbRate3Auto.CheckedChanged += cbRate3Auto_CheckedChanged;
            dataLog.Rows.Add("234:234:115:238:23:54:23:None");
            dataLog.Rows.Add("234:234:115:238:23:54:23:None");
        }

        void cbRate1Auto_CheckedChanged(object sender, EventArgs e)
        {
            if (cbRate1Auto.Checked)
            {
                cbRate1Auto.Checked = true;
                cbRate2Auto.Checked = true;
                cbRate3Auto.Checked = true;
            }
            else
            {
                cbRate1Auto.Checked = false;
                cbRate2Auto.Checked = false;
                cbRate3Auto.Checked = false;
            }
        }

        void cbRate2Auto_CheckedChanged(object sender, EventArgs e)
        {
            if (cbRate2Auto.Checked)
            {
                cbRate1Auto.Checked = true;
                cbRate2Auto.Checked = true;
                cbRate3Auto.Checked = true;
            }
            else
            {
                cbRate1Auto.Checked = false;
                cbRate2Auto.Checked = false;
                cbRate3Auto.Checked = false;
            }
        }

        void cbRate3Auto_CheckedChanged(object sender, EventArgs e)
        {
            if (cbRate3Auto.Checked)
            {
                cbRate1Auto.Checked = true;
                cbRate2Auto.Checked = true;
                cbRate3Auto.Checked = true;
            }
            else
            {
                cbRate1Auto.Checked = false;
                cbRate2Auto.Checked = false;
                cbRate3Auto.Checked = false;
            }
        }

        private void wbrBlog_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            switch (pos)
            {
                case "start":
                    index++;
                    txtIndex.Text = index.ToString();
                    pos = "login";
                    load = 1;
                    acc = arrAcc[index-1].ToString().Split(' ');
                    if (index == 1)
                    {
                        dataLog.Rows.Clear();
                    }
                    dataLog.Rows.Add("Loading: " + acc[0]);
                    wbrBlog.Document.Cookie.Remove(0, (wbrBlog.Document.Cookie.Count() - 1));
                    PostSignIn(acc[0], acc[1]);
                    break;
                case "login":
                    pos = "getdata";
                    wbrBlog.Navigate(txtLink.Text);
                    break;
                case "getdata":
                    string result = "";
                    string vote = "";
                    try
                    {
                        vote = wbrBlog.Document.GetElementById("cvotenum").InnerText;
                    }
                    catch (System.Exception ex)
                    {
                        vote = "0";
                    }
                    try
                    {
                        hash = wbrBlog.Document.GetElementById("blike2_" + bid).GetAttribute("signkey");
                        uid = wbrBlog.Document.GetElementById("buid_" + bid).GetAttribute("value");
                        string view = wbrBlog.Document.GetElementById("bv_" + bid).InnerText;
                        string like = wbrBlog.Document.GetElementById("detail_numlike").InnerText;
                        string com = wbrBlog.Document.GetElementById("detail_numcomment").InnerText;
                        string rate1 = wbrBlog.Document.GetElementById("bbbvote_1").InnerText;
                        string rate2 = wbrBlog.Document.GetElementById("bbbvote_2").InnerText;
                        string rate3 = wbrBlog.Document.GetElementById("bbbvote_3").InnerText;
                        if (Convert.ToInt32(view) <= 0 ||
                            Convert.ToInt32(like) <= 0 ||
                            Convert.ToInt32(com) <= 0 ||
                            Convert.ToInt32(rate1) <= 0 ||
                            Convert.ToInt32(rate2) <= 0 ||
                            Convert.ToInt32(rate3) <= 0)
                        {
                            throw new Exception("getdata");
                        }
                        load++;
                        /*
                        result += wbrBlog.Document.GetElementById("bv_" + bid).InnerText + " ";
                        result += "L: " + wbrBlog.Document.GetElementById("detail_numlike").InnerText + " ";
                        result += "V: " + vote + " ";
                        result += "C: " + wbrBlog.Document.GetElementById("detail_numcomment").InnerText + " ";
                        result += "R: " + wbrBlog.Document.GetElementById("bbbvote_1").InnerText + " - ";
                        result += wbrBlog.Document.GetElementById("bbbvote_2").InnerText + " - ";
                        result += wbrBlog.Document.GetElementById("bbbvote_3").InnerText;
                        dataLog.Rows.Add(index, acc[0], result);
                        */
                        if (load < 4)
                        {
                            throw new Exception("load");
                        }
                        dataLog.Rows.Add(view + ":" + like + ":" + vote + ":" + com + ":" +
                                                rate1 + ":" + rate2 + ":" + rate3);
                        pos = "like";
                    }
                    catch (System.Exception ex)
                    {
                        //MessageBox.Show(ex.ToString());
                    }
                    break;
                case "like":
                case "vote":
                case "random":
                case "comment":
                case "share":
                    timeAction.Start();
                    break;
                case "pause":
                    goto default;
                case "stop":
                    index = 0;
                    txtIndex.Text = "1";
                    goto default;
                case "end":
                    btnStart.Enabled = true;
                    btnStop.Enabled = false;
                    btnPause.Enabled = false;
                    MessageBox.Show("success");
                    break;
                case "logout":
                    if (index >= arrAcc.Count)
                    {
                        dataLog.Rows.Add("End!");
                        pos = "end";
                        index = 0;
                    }
                    else
                    {
                        //dataLog.Rows.Add(index, acc[0], "Logout...");
                        pos = "start";
                    }
                    wbrBlog.Navigate("https://pay.zing.vn/home/logout.html");
                    break;
                default:
                    break;
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            wbrBlog.Visible = !wbrBlog.Visible;
        }

        private void btnLog_Click(object sender, EventArgs e)
        {
            dataLog.Visible = !dataLog.Visible;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timeAction.Stop();
            txtIndex.Text = "1";
            btnStop.Enabled = false;
            btnPause.Enabled = false;
            btnStart.Enabled = true;
            ccbList.Enabled = true;
            txtLink.Enabled = true;
            txtBid.Enabled = true;
            txtOwnerName.Enabled = true;
            pos = "stop";
            wbrBlog.Navigate("about:blank");
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = true;
            index--;
            timeAction.Stop();
            pos = "pause";
            wbrBlog.Navigate("about:blank");
        }

        public void SendPost(string url, string strPostData)
        {
            byte[] bytePostData = Encoding.UTF8.GetBytes(strPostData);
            string strAdditionalHeaders = "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine;
            wbrBlog.Navigate(url, "", bytePostData, strAdditionalHeaders);
        }

        private void timeAction_Tick(object sender, EventArgs e)
        {
            timeAction.Stop();
            switch (pos)
            {
                case "like":
                    pos = "vote";
                    if (cbLikeAuto.Checked)
                    {
                        PostLike();
                    }
                    else
                    {
                        goto case "vote";
                    }
                    break;
                case "vote":
                    pos = "random";
                    if (cbVoteAuto.Checked)
                    {
                        PostVote();
                    }
                    else
                    {
                        goto case "random";
                    }
                    break;
                case "random":
                    pos = "comment";
                    if (cbRate1Auto.Checked)
                    {
                        PostRandom();
                    }
                    else
                    {
                        goto case "comment";
                    }
                    break;
                case "comment":
                    pos = "share";
                    if (cbComAuto.Checked)
                    {
                        PostComment();
                    }
                    else
                    {
                        goto case "share";
                    }
                    break;
                case "share":
                    pos = "logout";
                    if (cbShareAuto.Checked)
                    {
                        PostShare();
                    }
                    else
                    {
                        wbrBlog.Navigate("about:blank");
                    }
                    break;
            }
        }

        public void PostLike()
        {
            string postData = "type=like&bid=" + bid + "&hash=" + hash + "&ownerName=" + ownerName;
            SendPost("http://me.zing.vn/zb/ajx/like", postData);
        }

        public void PostVote()
        {
            string postData = "blog_id=" + bid + "&campaign_id=26&action=set";
            SendPost("http://me.zing.vn/zb/ajx/cpvote", postData);
        }

        public void PostRandom()
        {
            Random r = new Random();
            int rInt = r.Next(1, 4);
            string postData = "bid=" + bid + "&uid=" + uid + "&rate=" + rInt + "&from=bvote&ownerName=" + ownerName;
            SendPost("http://me.zing.vn/zb/ajx/like", postData);
        }

        public void PostComment()
        {
            arrShare = ShuffleAcc(arrShare);
            string comment = (string)arrShare[5];
            string postData = "bid=" + bid + "&fid=0&uid=" + uid + "&content=" + comment + "&ownerName=" + ownerName;
            SendPost("http://me.zing.vn/zb/ajx/postcmt", postData);
        }

        public void PostShare()
        {
            arrShare = ShuffleAcc(arrShare);
            string comment = (string)arrShare[5];
            string postData = "blogid=" + bid + "&text_status=" + comment + "&ownerName=" + ownerName;
            SendPost("http://me.zing.vn/zb/ajx/sharelink", postData);
        }

        public void PostSignIn(string u, string p)
        {
            string postData = "u=" + u + "&p=" + p +
                    "&x=57&y=20&u1=https://pay.zing.vn/newsso.php?section=home_index" +
                    "&pid=12&fp=https://pay.zing.vn/newsso.php?section=home_index";
            SendPost("https://sso3.zing.vn/login", postData);
        }

        public ArrayList ReadFile(string path)
        {
            ArrayList accounts = new ArrayList();
            try
            {
                string[] lines = System.IO.File.ReadAllLines(path);
                foreach (string line in lines)
                {
                    accounts.Add(line);
                }
            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show("Không tìm thấy file '" + path + "'", "Message");
                Environment.Exit(0);
            }
            return accounts;
        }

        private ArrayList ShuffleAcc(ArrayList inputList)
        {
            ArrayList randomList = new ArrayList();

            Random r = new Random();
            int randomIndex = 0;
            while (inputList.Count > 0)
            {
                randomIndex = r.Next(0, inputList.Count); //Choose a random object in the list
                randomList.Add(inputList[randomIndex]); //add it to the new, random list
                inputList.RemoveAt(randomIndex); //remove to avoid duplicates
            }

            return randomList; //return the new random list
        }

        private void dataLog_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            dataLog.FirstDisplayedScrollingRowIndex = dataLog.RowCount - 1;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            pos = "start";
            btnStart.Enabled = false;
            btnPause.Enabled = true;
            btnStop.Enabled = true;
            ccbList.Enabled = false;
            txtLink.Enabled = false;
            txtBid.Enabled = false;
            txtOwnerName.Enabled = false;
            bid = txtBid.Text.Trim();
            ownerName = txtOwnerName.Text.Trim();
            wbrBlog.Navigate("http://login.me.zing.vn");
        }

        public Image stringToImage(string inputString)
        {
            byte[] imageBytes = Encoding.Unicode.GetBytes(inputString);
            MemoryStream ms = new MemoryStream(imageBytes);

            return new Bitmap(ms);
        }

        private void dataLog_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex > 0 && e.Value != null && !e.Value.ToString().Contains("Loading") && !e.Value.ToString().Contains("End"))
            {
                e.PaintBackground(e.ClipBounds, false);
                //dataGridView1[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString();
                //PointF p = e.CellBounds.Location;
                //p.X += imageList1.ImageSize.Width;

                string[] test = e.Value.ToString().Split(':');
                e.Graphics.DrawImage(imageList.Images[0], e.CellBounds.X + 10, e.CellBounds.Y + 3, 16, 16);
                e.Graphics.DrawString(test[0], e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 25, e.CellBounds.Y + 8);

                e.Graphics.DrawImage(imageList.Images[1], e.CellBounds.X + 55, e.CellBounds.Y + 3, 16, 16);
                e.Graphics.DrawString(test[1], e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 70, e.CellBounds.Y + 8);

                e.Graphics.DrawImage(imageList.Images[2], e.CellBounds.X + 95, e.CellBounds.Y + 3, 16, 16);
                e.Graphics.DrawString(test[2], e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 110, e.CellBounds.Y + 8);

                e.Graphics.DrawImage(imageList.Images[3], e.CellBounds.X + 135, e.CellBounds.Y + 3, 16, 16);
                e.Graphics.DrawString(test[3], e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 150, e.CellBounds.Y + 8);

                e.Graphics.DrawImage(imageList.Images[4], e.CellBounds.X + 175, e.CellBounds.Y + 3, 16, 16);
                e.Graphics.DrawString(test[4], e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 190, e.CellBounds.Y + 8);

                e.Graphics.DrawImage(imageList.Images[5], e.CellBounds.X + 215, e.CellBounds.Y + 3, 16, 16);
                e.Graphics.DrawString(test[5], e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 230, e.CellBounds.Y + 8);

                e.Graphics.DrawImage(imageList.Images[6], e.CellBounds.X + 255, e.CellBounds.Y + 3, 16, 16);
                e.Graphics.DrawString(test[6], e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 270, e.CellBounds.Y + 8);

                e.Graphics.DrawImage(imageList.Images[7], e.CellBounds.X + 295, e.CellBounds.Y + 3, 16, 16);
                e.Graphics.DrawString(test[7], e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 310, e.CellBounds.Y + 8);
                e.Handled = true;
            }
        }

        private void ccbList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ccbList.SelectedIndex == 0)
            {
                arrAcc = ReadFile("acc.txt");
                arrAcc = ShuffleAcc(arrAcc);
                arrShare = ReadFile("share.txt");
            }
            else if (ccbList.SelectedIndex == 1)
            {
                arrAcc = ReadFile("acc1.txt");
                arrAcc = ShuffleAcc(arrAcc);
                arrShare = ReadFile("share1.txt");
            }
            txtAccount.Text = arrAcc.Count.ToString();
            txtText.Text = arrShare.Count.ToString();
            txtIndex.Text = "1";
        }
    }
}
